﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Business.Users;
using iOak.Domain.Users;
using iOak.Business.Security;

namespace iOak.Web.Administration
{
    public partial class UserEdit : BasePage
    {
        private UserService _userService = new UserService();
        private EncryptionService _encryptionService = new EncryptionService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (this.Request.QueryString["UserId"] != null)
                {
                    BindData();
                }
            }
        }

        private void BindData()
        {
            Guid userId = Guid.Parse(this.Request.QueryString["UserId"]);
            var user = _userService.GetUserById(userId);
            txtEmail.Text = user.Email;
            txtFullName.Text = user.FullName;
            txtMobile.Text = user.Mobile;
            txtPhone.Text = user.Phone;
            txtUserName.Text = user.UserName;
            chkIsActive.Checked = user.Active;
            chkIsAdmin.Checked = user.IsAdmin;
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            bool isCreate = true;
            User user = null;
            if (this.Request.QueryString["UserId"] != null)
            {
                isCreate = false;
                Guid userId = Guid.Parse(this.Request.QueryString["UserId"]);
                user = _userService.GetUserById(userId);
            }
            else
            {
                user = new User();
                user.UserId = Guid.NewGuid();
            }
            if (ValidateData(user))
            {
                user.Email = txtEmail.Text;
                user.FullName = txtFullName.Text;
                user.Mobile = txtMobile.Text;
                user.Phone = txtPhone.Text;
                user.UserName = txtUserName.Text;
                user.Active = chkIsActive.Checked;
                user.IsAdmin = chkIsAdmin.Checked;
                //如果输入了密码，则更新密码
                if (!String.IsNullOrWhiteSpace(txtPassword.Text)) 
                {
                    string saltkey = _encryptionService.CreateSaltKey(5);
                    user.SaltKey = saltkey;
                    user.PasswordHash = _encryptionService.CreatePasswordHash(txtPassword.Text, saltkey);
                }
                if (isCreate)
                {
                    _userService.RegisterUser(user);
                }
                else
                {
                    _userService.Update(user);
                }
                this.Response.Redirect("UserList.aspx");
            }

        }

        protected bool ValidateData(User checkUser)
        {
            var user = _userService.GetUserByName(txtUserName.Text.Trim());
            if (user != null && !user.UserId.Equals(checkUser.UserId))
            {
                this.Parent.Page.ClientScript.RegisterStartupScript(Page.GetType(), "showWarnning", "$(document).ready(function(){jAlert('用户名已存在！');});", true);
                return false;
            }
            return true;
        }
    }
}